package com.itheima.day09_atm;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    //创建一个存储账户的集合，集合中存储Account对象
    private ArrayList<Account> accounts = new ArrayList<Account>();
    private Scanner scanner = new Scanner(System.in);

    private Account loginAccount;

    public void start() {
        while (true) {
            System.out.println("=============ATM系统菜单================");
            System.out.println("1. 登录");
            System.out.println("2. 开户");
            System.out.println("请选择");
            int command = scanner.nextInt();
            switch (command) {
                case 1:
                    System.out.println("登录");
                    login();
                    break;
                case 2:
                    System.out.println("开户");
                    addAccount();
                    break;
                default:
                    System.out.println("您输入的命令有误，请重新输入！");
            }
        }
    }


    /**
     * 添加账户
     */
    private void addAccount() {
        System.out.println("开户");
        //1.创建Account对象
        Account account = new Account();

        System.out.println("请输入您的用户名：");
        String name = scanner.next();
        account.setUserName(name);

        char sex = 0;
        while (true) {
            System.out.println("请输入您的性别：");
            sex = scanner.next().charAt(0);
            if (sex == '男' || sex == '女') {
                account.setSex(sex);
                break;
            } else {
                System.out.println("您输入的性别有误，请重新输入！");
            }
        }
        while (true) {
            System.out.println("请输入您的密码：");
            String password = scanner.next();
            System.out.println("请再次输入您的密码：");
            String password2 = scanner.next();
            if (password.equals(password2)) {
                account.setPassword(password);
                break;
            } else {
                System.out.println("您输入的密码不一致，请重新输入！");
            }
        }
        System.out.println("请输入您的取现额度：");
        double limit = scanner.nextDouble();
        account.setLimit(limit);

        // 重点：我们需要为这个账户生成一个卡号（由系统自动生成。8位数字表示，不能与其他账户的卡号重复：会在下节课详细讲解）
        //TODO 这里先留着，待会把生成卡号的功能写好，再到这里调用
        String cardId = generateCardId();
        account.setCardId(cardId);

        //3.添加到集合中
        accounts.add(account);
        System.out.println("恭喜您，开户成功！您的卡号是：" + account.getCardId() + ",恭喜您" + account.getUserName());

    }

    // 登录功能
    private void login() {
        System.out.println("登录");
        if (accounts.size() == 0) {
            System.out.println("当前没有账户，请先开户！");
            return;
        }
        while (true) {
            System.out.println("请输入您的卡号：");
            String cardId = scanner.next();
            Account account = getAccountByCardId(cardId);
            if (account == null) {
                System.out.println("此卡号不存在！");
            } else {
                while (true) {
                    System.out.println("请输入您的密码：");
                    String passWord = scanner.next();
                    if (account.getPassword().equals(passWord)) {
                        System.out.println("登录成功！" + account.getUserName() + "您的卡号是：" + account.getCardId());
                        //TODO 登录成功，进入菜单
                        loginAccount = account;
                        showOperation();
                        return;
                    } else {
                        System.out.println("密码错误！");
                    }
                }
            }
        }
    }

    /**
     * 根据卡号查询账户
     *
     * @param cardId
     * @return
     */
    private Account getAccountByCardId(String cardId) {
        //1.遍历集合，获取每一个账户
        for (int i = 0; i < accounts.size(); i++) {
            Account account = accounts.get(i);
            //2.判断卡号是否一致
            if (account.getCardId().equals(cardId)) {
                return account;
            }
        }
        return null;
    }

    private String generateCardId() {
        while (true) {
            //1.生成8位随机数
            String cardId = "";
            Random random = new Random();
            for (int i = 0; i < 8; i++) {
                //获取一个随机数
                int number = random.nextInt(10);
                //拼接
                cardId += number;
            }
            //2.判断,集合中是否有这个卡号,
            Account account = getAccountByCardId(cardId);
            if (account == null) {
                //3.没有,返回,
                return cardId;
            }
        }
    }

    /**
     * 展示登录后操作界面
     */
    private void showOperation() {
        while (true) {
            System.out.println("=============ATM系统登录成功===============");
            System.out.println("---" + loginAccount.getUserName() + ",您可以选择如下功能进行账户的处理---");
            System.out.println("1.查询账户");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.修改密码");
            System.out.println("6.退出");
            System.out.println("7.注销当前账户");
            System.out.println("请选择操作：");
            int command = scanner.nextInt();
            switch (command) {
                case 1:
                    // TODO 查询账户
                    showLoginAccount();
                    break;
                case 2:
                    //TODO 存款
                    depositMoney();
                    break;
                case 3:
                    //TODO 取款
                    drawMoney();
                    break;
                case 4:
                    //TODO 转账
                    transferMoney();
                    break;
                case 5:
                    //TODO 修改密码
                    changePassword();
                    return;
                case 6:
                    // TODO 退出
                    System.out.println(loginAccount.getUserName() + "已退出");
                    return;
                case 7:
                    //注销当前登录的账户
                    if (deleteAccount()) {
                        System.out.println("注销成功！");
                        return;
                    }
                    break;
                default:
                    System.out.println("输入错误！");


            }
        }
    }

    private boolean deleteAccount() {
        System.out.println("----------注销操作----------");
        System.out.println("是否确认注销：y/n");
        String command = scanner.next();
        switch (command) {
            case "y":
                if (loginAccount.getMoney() == 0) {
                    accounts.remove(loginAccount);
                    System.out.println("注销成功！");
                    return true;
                } else {
                    System.out.println("注销失败！您的账户有未取出的余额！");
                    return false;
                }
            default:
                System.out.println("好的，您的账户继续保留！");
                return false;
        }
    }

    private void changePassword() {
        System.out.println("----------取款操作----------");
        while (true) {
            System.out.println("请输入您的密码：");
            String oldPassword = scanner.next();
            if (oldPassword.equals(loginAccount.getPassword())) {
                while (true) {
                    System.out.println("请输入新密码：");
                    String newPassword = scanner.next();
                    System.out.println("请再次输入新密码：");
                    String newPassword2 = scanner.next();
                    if (newPassword.equals(newPassword2)) {
                        loginAccount.setPassword(newPassword);
                        System.out.println("修改密码成功！");
                        return;
                    } else {
                        System.out.println("两次输入的密码不一致！");
                    }
                }
            } else {
                System.out.println("密码错误！");
            }
        }
    }

    private void transferMoney() {
        System.out.println("----------取款操作----------");
        //1.判断系统中是否存在其他账户
        if (accounts.size() <= 1) {
            System.out.println("系统中没有其他账户！");
            return;
        }
        //2.判断自己的账户是否有钱
        if (loginAccount.getMoney() <= 0) {
            System.out.println("账户余额不足！");
            return;
        }
        while (true) {
            System.out.println("请您输入对方卡号：");
            String cardId = scanner.next();
            if (cardId.equals(loginAccount.getCardId())) {
                System.out.println("不能给自己转钱！");
                return;
            } else {
                Account account = getAccountByCardId(cardId);
                if (account == null) {
                    System.out.println("对方账户不存在！");
                    return;
                } else {
                    System.out.println("对方账户存在！");
                    String name = "*" + account.getUserName().substring(1);
                    System.out.println("对方账户名称：" + name);
                    String preName = scanner.next();
                    if (account.getUserName().startsWith(preName)) {
                        System.out.println("请输入转账金额：");
                        double money = scanner.nextDouble();
                        if (loginAccount.getMoney() >= money) {
                            loginAccount.setMoney(loginAccount.getMoney() - money);
                            account.setMoney(account.getMoney() + money);
                            System.out.println("转账成功！");
                            return;
                        } else {
                            System.out.println("余额不足！" + "最多可转" + loginAccount.getMoney());
                        }
                    } else {
                        System.out.println("认证用户失败！");
                    }
                    break;
                }
            }
        }
    }

    private void drawMoney() {
        System.out.println("----------取款操作----------");
        if (loginAccount.getMoney() < 100) {
            System.out.println("余额不足！");
            return;
        }
        while (true) {
            System.out.println("请输入取款金额：");
            double money = scanner.nextDouble();

            if (loginAccount.getMoney() >= money) {
                if (money > loginAccount.getLimit()) {
                    System.out.println("超出取款额度！");
                } else {
                    loginAccount.setMoney(loginAccount.getMoney() - money);
                    System.out.println("取款成功！余额为：" + loginAccount.getMoney());
                    break;
                }
            } else {
                System.out.println("余额不足！");
            }

        }

    }

    private void depositMoney() {
        System.out.println("----------存款操作----------");
        System.out.println("请输入存款金额：");
        double money = scanner.nextDouble();
        loginAccount.setMoney(loginAccount.getMoney() + money);
        System.out.println("存款成功！" + "存了" + money + "余额为：" + loginAccount.getMoney());
    }

    /**
     * 显示登录的账户信息
     */
    private void showLoginAccount() {
        System.out.println("----------账户信息----------");
        System.out.println("卡号：" + loginAccount.getCardId());
        System.out.println("用户名：" + loginAccount.getUserName());
        System.out.println("性别：" + loginAccount.getSex());
        System.out.println("余额：" + loginAccount.getMoney());
        System.out.println("额度：" + loginAccount.getLimit());

    }
}
